/**
 * 
 */
package com.gcgt.client;

import java.math.BigDecimal;

import com.gcgt.shared.trader.HistoryItem;
import com.gcgt.shared.trader.TradingHistory;
import com.gcgt.shared.trader.TradingParameters;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DatePicker;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.visualizations.AnnotatedTimeLine;

/**
 * @author Vincentas
 * 
 */
public class FinanceUI extends Composite {

	private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);

	private static FinanceUIUiBinder uiBinder = GWT.create(FinanceUIUiBinder.class);

	interface FinanceUIUiBinder extends UiBinder<Widget, FinanceUI> {
	}

	@UiField(provided = true)
	final AnnotatedTimeLine timeLine;

	@UiField
	Button button;

	@UiField
	DatePicker fromDate;

	@UiField
	DatePicker toDate;

	@UiField
	TextBox cash;

	@UiField
	TextBox commision;

	@UiField
	TextBox days;

	@UiField
	TextBox depositPercent;

	@UiField
	TextBox maxLossPercent;
	
	@UiField
	DialogBox dialog;

	@UiHandler("button")
	public void HandleButton(ClickEvent clickEvent) {
		dialog.show();
		
		TradingParameters parameters = new TradingParameters();
		parameters.setCash(new BigDecimal(cash.getValue()));
		parameters.setCommision(new BigDecimal(commision.getValue()));
		parameters.setDays(Integer.valueOf(days.getValue()));
		parameters.setDepositPercent(new BigDecimal(depositPercent.getValue()));
		parameters.setMaxLossPercent(new BigDecimal(maxLossPercent.getValue()));
		parameters.setStartDate(fromDate.getValue());
		parameters.setEndDate(toDate.getValue());

		greetingService.getTradingHistory(parameters, new AsyncCallback<TradingHistory>() {

			@Override
			public void onSuccess(TradingHistory result) {

				DataTable dataTable = DataTable.create();
				dataTable.addColumn(ColumnType.DATETIME);
				dataTable.addColumn(ColumnType.NUMBER, "Total");
				dataTable.addColumn(ColumnType.NUMBER, "Cash");
				dataTable.addRows(result.size());
				for (int i = 0; i < result.size(); i++) {
					HistoryItem item = result.get(i);
					dataTable.setValue(i, 0, item.getDate());
					dataTable.setValue(i, 1, item.getTotal().doubleValue());
					dataTable.setValue(i, 2, item.getCash().doubleValue());
				}

				timeLine.draw(dataTable);
				
				dialog.hide();
			}

			@Override
			public void onFailure(Throwable caught) {
				dialog.hide();
				Window.alert("fail " + caught.getMessage());
			}
		});
	}

	public FinanceUI() {
		timeLine = new AnnotatedTimeLine("800px", "600px");
		initWidget(uiBinder.createAndBindUi(FinanceUI.this));
	}

}
